var System=Java.type("java.lang.System");
var Cookie=Java.type("javax.servlet.http.Cookie");
var Date=Java.type("java.util.Date");
var ArrayList=Java.type("java.util.ArrayList");
var HashMap=Java.type("java.util.HashMap");
var HashSet=Java.type("java.util.HashSet");
var String=Java.type("java.lang.String");
var Calendar=Java.type("java.util.Calendar");
var URLEncoder=Java.type("java.net.URLEncoder");
var File=Java.type("java.io.File");
var Arrays=java.util.Arrays;
//jse
var Record=Java.type("com.jse.entity.Record");
var Lang=Java.type("com.jse.util.Lang");
var Times=Java.type("com.jse.util.Times");
var Cnd=Java.type("com.jse.dao.Cnd");
var Chain=Java.type("com.jse.util.JseMap");
var JseMap=Java.type("com.jse.util.JseMap");
var Pager=Java.type("com.jse.dao.Pager");
var Http=Java.type("com.jse.util.Http");
var Json=Java.type("com.jse.util.Json");
var IP=Java.type("com.jse.util.IP");
var Captcha=Java.type("com.jse.captcha.Captcha");
/** dao begin */
var $list=function (table,cnd,page,num){
	if(isEmpty(cnd)){cnd=null;}else if(typeof(cnd)=="string"){cnd=Cnd.wrap(cnd)}
	if(isEmpty(page)){return dao.query(table,cnd)}
	if(typeof(page)=="object"){return dao.query(table,cnd,page)}
	var pager=new Pager(page,num)
	return dao.query(table,cnd,pager)
}
var $pager=function (table,cnd,page,num){if(typeof(page)=="object") return dao.pager(table,cnd,page); else return dao.pager(table,cnd,page,num);}
var $fetch=function (table,cnd){
	if(isEmpty(cnd)){cnd=null;}else if(typeof(cnd)=="string"){cnd=Cnd.wrap(cnd)}
	return dao.fetch(table,cnd);
}
var $count=function(table,cnd){if(isEmpty(cnd)){cnd=null;}return dao.count(table,cnd)}
var $sql=function(sql,type){return dao.sql(sql,type);}
var $func=function(table,func,col){return dao.func(table, func, col)}
var $save=function (tbl){
	tbl=JseMap.WRAP(tbl)
	if(isEmpty(tbl.id))
		tbl.remove("id")
	if(tbl._httpurl!=null){
		attr("_httpurl",tbl._httpurl)
		tbl.remove("_httpurl")
	}
	try {
		dao.insert(tbl)
	} catch (e) {
		print(e)
		if("Field 'id' doesn't have a default value"==e){
			tbl.put("id",uuid())
			dao.insert(tbl)
		}
	}
	return tbl;
}
var $update=function (tbl,cnd){
	tbl=JseMap.WRAP(tbl)
	if(tbl._httpurl!=null){
		attr("_httpurl",tbl._httpurl)
		tbl.remove("_httpurl")
	}
	if(tbl.containsKey(".table")){
		table=tbl.get(".table");
		tbl.remove(".table")
		if(isEmpty(cnd)){cnd=Cnd.where("id","=",tbl.id);}else if(typeof(cnd)=="string"){cnd=Cnd.wrap(cnd)}
		return dao.update(table,Chain.from(tbl),cnd)
	}
	return -1;
}
var $mager=function (tbl,cnd){if(isEmpty(tbl.id)){tbl.remove("id");$save(tbl);}else{$update(tbl,cnd)}}
function $del(tbl,id){
	var sql=Sqls.create("DELETE FROM $table WHERE id = @id");
	sql.vars().set("table",tbl);
	sql.params().set("id",id);
	return dao.execute(sql);
}
/** dao end! **/
function isEmpty(v){return v==undefined||v==null||v==""||v=="null"}
var print=function(x){System.err.println("jjs:->"+req.getServletPath()+" print >>:"+x)}
var reqBody= function(){return Streams.readAndClose(req.getReader())}
var foJson=function(x){if((typeof x)=="object")return x;else return Json.fromJson(x);}
var toJson=function(x){return Json.toJson(x)}
var attr=function(n,v){if(v==undefined){return req.getAttribute(n)}req.setAttribute(n,v)}
var sattr=function(n,v){if(v==undefined){return session.getAttribute(n)}session.setAttribute(n,v)}
var $app=function(n,v){if(v==undefined){return app.getAttribute(n)}app.setAttribute(n,v)}
var user=function(){return sattr("user")}
var uuid=function(){
	var str = java.util.UUID.randomUUID().toString()
	var temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
	return temp;
}
function addCookie(n,v){
	var cookie = new Cookie(n,v);
	cookie.setMaxAge(31536000000);
	cookie.setDomain(domain);//www为域名
	cookie.setPath("/");
	resp.addCookie(cookie);
}
function delCookie(){
	var cookies = req.getCookies();
	for (var i = 0; i < cookies.length; i++) {
		cookies[i].setMaxAge(0);
		cookies[i].setPath("/");
		cookies[i].setDomain(domain);
		resp.addCookie(cookies[i]);
	}
}
var ip=function(){return IP.getIP(req);}
var isAjax=function(){let type = req.getHeader("X-Requested-With");if(!isEmpty(type)&&type=="XMLHttpRequest"){return true;}else return false;}